/*
 * JPanelWizardLibreSelectElements.java
 *
 * Created on 29-ago-2011, 16:23:22
 */
package com.ufps.cuatro.vistas.asistente.libre.wizard;

import java.util.ArrayList;
import java.util.Observable;
import java.util.Observer;

import javax.swing.ImageIcon;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListDataListener;

import org.jdesktop.application.Action;

import com.ufps.cuatro.CuatroApp;
import com.ufps.cuatro.modelo.asistente.libre.wizard.JPanelWizardLibreSelectElementsDescriptor;
import com.ufps.cuatro.modelo.representacion.componente.ComponenteDistribuidor;
import com.ufps.cuatro.modelo.representacion.componente.ComponenteDistribuidorExtra;
import com.ufps.cuatro.modelo.representacion.componente.ComponenteGeneradoSimple;
import com.ufps.cuatro.modelo.representacion.componente.ComponenteDistribuidorIntegrador;
import com.ufps.cuatro.modelo.representacion.componente.ComponenteDistribuidorIntegradorExtra;
import com.ufps.cuatro.util.Bandera;
import com.ufps.cuatro.util.Utileria;
import com.ufps.cuatro.util.render.MyListRendererJLabel;
import com.ufps.cuatro.util.wizard.UtilWizard;
import com.ufps.cuatro.vistas.JDialogCrearComponenteManual;
import com.ufps.cuatro.vistas.representacion.JDialogSeleccionIntegradores;

/**
 *
 * @author Carlos A. Torres C. 
 */
public class JPanelWizardLibreSelectElements extends javax.swing.JPanel implements Observer {
    private static final long serialVersionUID = 3804509231290149123L;
    
    private ArrayList<ComponenteDistribuidor> objElementos;
    private JPanelWizardLibreSelectElementsDescriptor objDescriptor;

    /** Creates new form JPanelWizardLibreSelectElements */
    public JPanelWizardLibreSelectElements() {
        initComponents();
        objElementos = new ArrayList<ComponenteDistribuidor>(); 
    }
    
    /** Creates new form JPanelWizardLibreSelectElements */
    public JPanelWizardLibreSelectElements(JPanelWizardLibreSelectElementsDescriptor objDescriptor) {
        initComponents();
        objElementos = new ArrayList<ComponenteDistribuidor>(); 
        this.objDescriptor = objDescriptor;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jListElementos = new javax.swing.JList();
        jLabel3 = new javax.swing.JLabel();
        jLabelInfoElemento = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jToolBar1 = new javax.swing.JToolBar();
        jButton1 = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JToolBar.Separator();
        jButton2 = new javax.swing.JButton();
        jSeparator2 = new javax.swing.JToolBar.Separator();
        jButton4 = new javax.swing.JButton();
        jSeparator3 = new javax.swing.JToolBar.Separator();
        jButton3 = new javax.swing.JButton();

        setName("Form"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance().getContext().getResourceMap(JPanelWizardLibreSelectElements.class);
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jListElementos.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jListElementos.setName("jListElementos"); // NOI18N
        jListElementos.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                jListElementosValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(jListElementos);

        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        jLabelInfoElemento.setText(resourceMap.getString("jLabelInfoElemento.text")); // NOI18N
        jLabelInfoElemento.setName("jLabelInfoElemento"); // NOI18N

        jLabel4.setBackground(resourceMap.getColor("jLabel4.background")); // NOI18N
        jLabel4.setIcon(resourceMap.getIcon("jLabel4.icon")); // NOI18N
        jLabel4.setText(resourceMap.getString("jLabel4.text")); // NOI18N
        jLabel4.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jLabel4.setName("jLabel4"); // NOI18N
        jLabel4.setOpaque(true);

        jToolBar1.setFloatable(false);
        jToolBar1.setOrientation(javax.swing.SwingConstants.VERTICAL);
        jToolBar1.setRollover(true);
        jToolBar1.setName("jToolBar1"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance().getContext().getActionMap(JPanelWizardLibreSelectElements.class, this);
        jButton1.setAction(actionMap.get("addElemento")); // NOI18N
        jButton1.setFocusable(false);
        jButton1.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButton1.setName("jButton1"); // NOI18N
        jButton1.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar1.add(jButton1);

        jSeparator1.setName("jSeparator1"); // NOI18N
        jToolBar1.add(jSeparator1);

        jButton2.setAction(actionMap.get("addIntegradorExtra")); // NOI18N
        jButton2.setFocusable(false);
        jButton2.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButton2.setName("jButton2"); // NOI18N
        jButton2.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar1.add(jButton2);

        jSeparator2.setName("jSeparator2"); // NOI18N
        jToolBar1.add(jSeparator2);

        jButton4.setAction(actionMap.get("addPlantilla")); // NOI18N
        jButton4.setFocusable(false);
        jButton4.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButton4.setName("jButton4"); // NOI18N
        jButton4.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar1.add(jButton4);

        jSeparator3.setName("jSeparator3"); // NOI18N
        jToolBar1.add(jSeparator3);

        jButton3.setAction(actionMap.get("delElemento")); // NOI18N
        jButton3.setFocusable(false);
        jButton3.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButton3.setName("jButton3"); // NOI18N
        jButton3.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar1.add(jButton3);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabelInfoElemento, javax.swing.GroupLayout.DEFAULT_SIZE, 658, Short.MAX_VALUE)
                    .addComponent(jLabel4, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 658, Short.MAX_VALUE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 658, Short.MAX_VALUE)
                    .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 658, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 605, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 177, Short.MAX_VALUE)
                    .addComponent(jToolBar1, javax.swing.GroupLayout.DEFAULT_SIZE, 177, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabelInfoElemento, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

private void jListElementosValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_jListElementosValueChanged
        if(!this.jListElementos.isSelectionEmpty()){
            this.infoIconLabel();
        }
}//GEN-LAST:event_jListElementosValueChanged

    private void infoIconLabel(){
        String strMSG = "";
        Object obj =  this.jListElementos.getSelectedValue();
        if(obj instanceof ComponenteGeneradoSimple){
            ComponenteGeneradoSimple objTempComponenteGenerado = (ComponenteGeneradoSimple) obj;
            strMSG = "Componente Generado: " + objTempComponenteGenerado.getStrClassPath();
            this.setIcono(Utileria.ICONO_COMPONENTE_GENERADO, strMSG);
        }
        else if(obj instanceof ComponenteDistribuidorExtra){
            ComponenteDistribuidorExtra objTempComponenteExtra = (ComponenteDistribuidorExtra) obj;
            strMSG = "Componente Extra: " + objTempComponenteExtra.getStrClassPath();
            this.setIcono(Utileria.ICONO_COMPONENTE_EXTRA, strMSG);
        }                        
        else if(obj instanceof ComponenteDistribuidorIntegradorExtra){
            ComponenteDistribuidorIntegradorExtra objTempComponenteIntegradorExtra = (ComponenteDistribuidorIntegradorExtra) obj;
            strMSG = "Componente Extra: " + objTempComponenteIntegradorExtra.getStrClassPath();
            this.setIcono(Utileria.ICONO_COMPONENTE_INTEGRADOR_EXTRA, strMSG);
        }
        else if(obj == null){
            this.setIcono(Utileria.ICONO_VACIO, strMSG);
            this.jLabelInfoElemento.setText("");
            this.jLabelInfoElemento.setIcon(null);
        }
    }

    private void setIcono(String key, String strMSG){
        key = key.replaceAll("16x16","32x32");
        ImageIcon icono = Utileria.leerRecursoImagen(key, 16);
        JLabel labelIcono = new JLabel(icono);
        this.jLabelInfoElemento.setIcon(labelIcono.getIcon());
        this.jLabelInfoElemento.setText(strMSG);
    }

    @Action
    public void addElemento() {               
        JDialogCrearComponenteManual objDialogCrearComponente = new JDialogCrearComponenteManual(this, Utileria.CREAR_COMPONENTE_EXTRA);
        JDialog objDialog = UtilWizard.objWizard.getDialog();
        objDialogCrearComponente.setLocationRelativeTo(objDialog);                
        CuatroApp.getApplication().show(objDialogCrearComponente);       
    }

    @Action
    public void delElemento() {
        if(this.objElementos.isEmpty()){
            Utileria.mostrarMensaje(this, "Debe Agregar Componentes.", "", JOptionPane.WARNING_MESSAGE);
        }
        if(this.jListElementos.isSelectionEmpty()){
            Utileria.mostrarMensaje(this, "Debe Seleccionar un Compoente.", "", JOptionPane.WARNING_MESSAGE);
        }
        if(!Utileria.confirmarOrden(this, "¿Desea Eliminar el Componente?", ""))return;        
        int i = this.jListElementos.getSelectedIndex();
        this.objElementos.remove(i);
        this.loadRendererJListInluidos();
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabelInfoElemento;
    private javax.swing.JList jListElementos;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JToolBar.Separator jSeparator1;
    private javax.swing.JToolBar.Separator jSeparator2;
    private javax.swing.JToolBar.Separator jSeparator3;
    private javax.swing.JToolBar jToolBar1;
    // End of variables declaration//GEN-END:variables

    public void update(Observable o, Object arg) {
        if(o instanceof Bandera){            
            Bandera objBandera = (Bandera) o;
            switch(objBandera.getEstado()){                
                case Bandera.COMPONENTE_INTEGRADOR_EXTRA_AGREGADO:
                    this.addComponente((ComponenteDistribuidorIntegradorExtra) objBandera.getObj());
                    break;
                case Bandera.COMPONENTE_EXTRA_CREADO:
                    this.addComponente((ComponenteDistribuidorExtra) objBandera.getObj());
                    break;
                case Bandera.COMPONENTE_GENERADO_CREADO:
                    this.addComponente((ComponenteGeneradoSimple) objBandera.getObj());
                    break; 
                case Bandera.SELECCION_COMPONENTES_INTEGRADORES:
                    this.addComponentes((ArrayList<ComponenteDistribuidorIntegrador>) objBandera.getObj());
                    break; 
            }                             
        }
    }

    public ArrayList<ComponenteDistribuidor> getObjElementos() {
        return objElementos;
    }

    @Action
    public void addGenerado() {        
        JDialogCrearComponenteManual objDialogCrearComponente = new JDialogCrearComponenteManual(this, Utileria.CREAR_COMPONENTE_GENERADO);
        objDialogCrearComponente.setLocationRelativeTo(UtilWizard.objWizard.getDialog());                
        CuatroApp.getApplication().show(objDialogCrearComponente);    
    }
    
    private void addComponentes(ArrayList<ComponenteDistribuidorIntegrador> objColeccionComponentes){
        for(ComponenteDistribuidorIntegrador objComponenteIntegrador : objColeccionComponentes){
            if(objComponenteIntegrador instanceof ComponenteDistribuidorIntegradorExtra){
                this.objElementos.add((ComponenteDistribuidorIntegradorExtra) objComponenteIntegrador);
            }
        }    
        loadRendererJListInluidos();
    }
    
    private void addComponente(ComponenteDistribuidorIntegradorExtra obj){
        this.objElementos.add(obj);    
        loadRendererJListInluidos();
    }
    
    private void addComponente(ComponenteDistribuidorExtra obj){
        this.objElementos.add(obj);    
        loadRendererJListInluidos();
    }            
    
    private void addComponente(ComponenteGeneradoSimple obj){                
        loadRendererJListInluidos();
    }  
    
     /**
     * Carga la Lista de <code>Componente</code>s Incluidos en la Distribución Actual.
     */
    public void loadRendererJListInluidos() {
        JList objLista = new JList(this.objElementos.toArray());
        this.jListElementos.setModel(objLista.getModel());
        this.jListElementos.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        
        MyListRendererJLabel objRender = new MyListRendererJLabel(0);
        this.jListElementos.setCellRenderer(objRender); 
        objDescriptor.setNextButtonAccordingToListEmpty();
         this.jLabelInfoElemento.setText("");
    }
    
    public boolean isListEmpty(){
        return this.jListElementos.getModel().getSize() == 0 ? true : false;
    }
    
    public void addListDataListener(ListDataListener i){
        this.jListElementos.getModel().addListDataListener(i);
    }

    @Action
    public void addIntegradorExtra() {
        JDialogSeleccionIntegradores objDialogSeleccionIntegradores = new JDialogSeleccionIntegradores(this);
        objDialogSeleccionIntegradores.setLocationRelativeTo(UtilWizard.objWizard.getDialog());                
        CuatroApp.getApplication().show(objDialogSeleccionIntegradores);       
    }

    @Action
    public void addPlantilla() {
    }
}
